package com.smart.manager.product.repository;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjUtil;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.option.SelectOption;
import com.smart.manager.product.entities.body.RPropsCombo;
import com.smart.manager.product.entities.enums.ProfileStatus;
import com.smart.manager.product.entities.models.PropsCombo;
import com.smart.manager.product.entities.models.query.QPropsCombo;
import com.smart.manager.product.entities.models.query.QPropsProfile;
import com.smart.manager.product.entities.views.PropsProfileView;
import com.smart.manager.product.entities.views.query.QPropsProfileView;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 游戏管理  套餐档案
 *
 * @author leo
 * @date 2023/09/08
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class PropsComboRepository {

    public Page<PropsCombo> pageList(RPropsCombo.SearchBody search) {
        QPropsCombo query = new QPropsCombo();
        Opt.ofBlankAble(search.getCode()).ifPresent(x -> query.code.contains(x));
        Opt.ofBlankAble(search.getStatus()).ifPresent(x -> query.status.eq(x));
        Opt.ofBlankAble(search.getName()).ifPresent(x -> query.name.contains(x));
        query.order().sort.asc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        Page<PropsCombo> load = new Page<PropsCombo>().load(query.findPagedList());
        for (PropsCombo record : load.getRecords()) {
            if (ObjUtil.isNotNull(record.getMeat())) {
                List<PropsProfileView> list = new QPropsProfileView().code.in(record.getMeat()).findList();
                List<String> collect = list.stream().map(PropsProfileView::getName).collect(Collectors.toList());
                record.setMeatName(collect);
                record.setMeats(list);
                record.setIcons(list.stream().map(PropsProfileView::getIcon).collect(Collectors.toList()));
            }
        }
        return load;
    }

    public void add(RPropsCombo.EditBody body) {
        Collections.sort(body.getMeat());

        PropsCombo record =  new QPropsCombo().code.eq(body.getCode()).setMaxRows(1).findOne();
        Assert.isNull(record, "创建失败: 套餐代码已存在!");

        if (body.getIsPack()){

            PropsCombo one = new QPropsCombo().isPack.eq(true).isOrganic.eq(body.getIsOrganic()).setMaxRows(1).findOne();

            Assert.isNull(one, "创建失败: 当提前有机属性下存在了相同的套餐包!");


        }


        PropsCombo combo = BeanUtil.copyProperties(body, PropsCombo.class);
        renewalIcon(combo).insert();
    }


    public void update(Long id, RPropsCombo.EditBody body) {
        Collections.sort(body.getMeat());
        QPropsCombo query = new QPropsCombo();
        PropsCombo record = query.id.eq(id).setMaxRows(1).findOne();
        Assert.notNull(record, ArcoTips.UPDATE_NOT_FOUND);
        BeanUtil.copyProperties(body, record);
        renewalIcon(Objects.requireNonNull(record)).update();
    }

    /**
     * 更新图标
     *
     * @param combo
     * @return {@link PropsCombo}
     */
    public PropsCombo renewalIcon(PropsCombo combo) {
        List<String> list = new QPropsProfile().select(QPropsProfile.Alias.icon).code.in(combo.getMeat()).findSingleAttributeList();
        combo.setIcons(list);
        return combo;
    }

    public int delByIds(List<Long> ids) {
        List<PropsCombo> list = new QPropsCombo().id.in(ids).findList();
        Assert.notEmpty(list, ArcoTips.DELETE_NOT_FOUND);

        return DB.deleteAll(list);


    }

    /**
     * 根据等级适用的套餐获取所有套餐
     *
     * @return {@link List}<{@link PropsCombo}>
     */
    public List<PropsCombo> getList(Integer level) {

        return new QPropsCombo().status.eq(ProfileStatus.ON).and().minLevel.ge(level).maxLevel.le(level).endAnd().findList();
    }

    /**
     * 获取所有套餐
     *
     * @return {@link List}<{@link PropsCombo}>
     */
    public List<PropsCombo> getAllList(Boolean isOrganic ) {
        List<PropsCombo> list = new QPropsCombo().isPack.eq(false).isOrganic.eq(isOrganic).status.eq(ProfileStatus.ON).sort.asc().findList();
        for (PropsCombo combo : list) {
            List<PropsProfileView> views = new QPropsProfileView().code.in(combo.getMeat()).findList();
            if (views.isEmpty()) {
                continue;
            }
            combo.setMeats(views);
            combo.setMeatName(views.stream().map(PropsProfileView::getName).collect(Collectors.toList()));
            combo.setIcons(views.stream().map(PropsProfileView::getIcon).collect(Collectors.toList()));
        }
        return list;
    }

    public PropsCombo getOne(Boolean isOrganic) {
        PropsCombo combo = new QPropsCombo().isPack.eq(true).isOrganic.eq(isOrganic).status.eq(ProfileStatus.ON).setMaxRows(1).findOne();
        log.warn("单独套餐  =  {}",combo);
        if (ObjUtil.isNotNull(combo)) {
            List<PropsProfileView> views = new QPropsProfileView().code.in(combo.getMeat()).findList();
            combo.setMeats(views);
            combo.setMeatName(views.stream().map(PropsProfileView::getName).collect(Collectors.toList()));
            combo.setIcons(views.stream().map(PropsProfileView::getIcon).collect(Collectors.toList()));
            return combo;
        }
        return null;
    }


    /**
     * 获取数据字典下拉选项
     *
     * @return {@code List<SelectOption>}
     */
    public List<SelectOption> getOptions() {
        QPropsCombo place = new QPropsCombo();
        return place.findStream()
                .map(x -> new SelectOption().setLabel(x.getName()).setValue(x.getCode()).setDisabled(false))
                .collect(Collectors.toList());
    }


}






